package robbie.WeatherApp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.ListActivity;
import android.content.*;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.os.Bundle;

public class WeatherAppActivity extends ListActivity {
	
	private final static String NOTES="notes.txt";
	
	
    OraseAdapter adapter ;
    Button b;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceBundle) 
    {
        super.onCreate(savedInstanceBundle);
        adapter = new OraseAdapter(this);
        setContentView(R.layout.main);
        setListAdapter(adapter);
        
    	try 
    	{
	    	InputStream in = openFileInput(NOTES);
	    	if (in != null) {
		    	InputStreamReader tmp = new InputStreamReader(in);
		    	BufferedReader reader = new BufferedReader(tmp);
		    	String str;
 
		    	while ((str = reader.readLine()) != null) 
		    	{
		    		adapter.adaugaOras(str);
		    	}
		    	in.close();


	    	}
    	} 
    	catch (java.io.FileNotFoundException e) 
    	{
    		// that's OK, we probably haven't created it yet
    	} catch (Throwable t) 
    	{
    	Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
    	}
        
        b = (Button)findViewById(R.id.MainButton);
        
        b.setOnClickListener(new View.OnClickListener() {
			

			public void onClick(View v) 
			{
				Intent adauga = new Intent();
				adauga.setClass(WeatherAppActivity.this, AddActivity.class);
				
				startActivityForResult(adauga,100);
				
				
			}
		});
        
        super.getListView().setOnItemClickListener(new OnItemClickListener() {


			public void onItemClick(AdapterView<?> listAdapter, View view, int position,long id) 
			{
        		Intent trimiteOras = new Intent();
        		trimiteOras.setClass(WeatherAppActivity.this, ShowInfoActivity.class);
        		
        		trimiteOras.putExtra("numeOras", adapter.getItem(position).nume);
				startActivity(trimiteOras);
        		
			}
        	

        });
        
        super.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
			

			public boolean onItemLongClick(AdapterView<?> listAdapter, View view, int position, long id) {
				

				Oras o = (Oras)adapter.getItem(position);
				adapter.stergeOras(o);
				
				return true;
			}
		});
        

        
    }	// END onCreate()
    
 
    public void onPause() 
    {
    	super.onPause();
    	try 
    	{
	    	OutputStreamWriter out = new OutputStreamWriter(openFileOutput(NOTES, 0));
	    	for(int i=0;i< adapter.getCount();i++)
	    	{
		    	out.write(adapter.getItem(i).nume+"\n");
	    	}
	    	out.close();
    	}
    	catch (Throwable t) 
    	{
    	Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
    	}
    }
    
    @Override
    public void onActivityResult (int requestCode, int responseCode, Intent data)
    {
    	// raspunsul vine de la formularul de adaugare
    	// acolo am cerut raspunsul cu nr 100
    	if (requestCode == 100)
    	{
    		if (responseCode==1)	    			// s-a apasat butonul Adauga
    		{	
    	        adapter.adaugaOras(data.getStringExtra("nume_oras"));
    		}
    		else if (responseCode==0)	    		// s-a apasat butonul Renunta
    		{
    		}
    	}
    }

/*    
    @Override
    public void onListItemClick (ListView list, View v, int position, long id)
    {
        // afisez numele orasului pe care s-a dat click folosind un Toast
        Oras p = (Oras)adapter.getItem (position);
        Toast.makeText(this, p.nume, Toast.LENGTH_LONG).show();
    }
*/    

    
    
}